package com.baizhi.lmy.service;

import com.baizhi.lmy.vo.CategoryPageVO;
import com.baizhi.lmy.dao.CategoryMapper;
import com.baizhi.lmy.dao.UserMapper;
import com.baizhi.lmy.entity.Category;
import com.baizhi.lmy.entity.CategoryExample;
import com.baizhi.lmy.entity.User;
import com.baizhi.lmy.entity.UserExample;
import com.baizhi.lmy.vo.CategoryPageVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public CategoryPageVO queryOnePage(int page, int pageSize) {
        CategoryPageVO categoryPageVO = new CategoryPageVO();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);
        int offset = (page-1)*pageSize;
        //分页查询数据
        RowBounds rowBounds = new RowBounds(offset,pageSize);
        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(categoryExample,rowBounds);
        int total = categoryMapper.selectCountByExample(categoryExample);
        categoryPageVO.setPage(page);
        categoryPageVO.setTotal(total);
        categoryPageVO.setRows(categoryList);
        return categoryPageVO;
    }
//二级类别
    @Override
    public CategoryPageVO queryTwoPage(int page, int pageSize,int categoryId) {
        CategoryPageVO categoryPageVO = new CategoryPageVO();
        //计算分页时的起始行下标
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentIdEqualTo(categoryId);
        int offset = (page-1)*pageSize;
        //分页查询数据
        RowBounds rowBounds = new RowBounds(offset,pageSize);
        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(categoryExample,rowBounds);
        int total = categoryMapper.selectCountByExample(categoryExample);
        categoryPageVO.setPage(page);
        categoryPageVO.setTotal(total);
        categoryPageVO.setRows(categoryList);
        return categoryPageVO;
    }
    //查询一级类别
    @Override
    public List<Category> queryByLevelsCategory(int levels) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(levels);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        return categoryList;
    }

    //添加
    @Override
    public Map<String,Object> register(String cateName,Integer parentId) {
        Map<String, Object> map = new HashMap<>();
        if (parentId == null) {
            int i = categoryMapper.insertSelective(new Category(null, cateName, 1, parentId));
            if (i == 1) {
                map.put("message", "添加成功");
                map.put("status", 200);
                return map;
            } else {
                map.put("message", "添加失败");
                map.put("status", 400);
                return map;
            }
        } else {
            int i = categoryMapper.insertSelective(new Category(null, cateName, 2, parentId));
            if (i == 1) {
                map.put("message", "添加成功");
                map.put("status", 200);
                return map;
            } else {
                map.put("message", "添加失败");
                map.put("status", 400);
                return map;
            }
        }
    }


    //删除
    @Override
    public Map<String,Object> delete(int id, Integer parentId) {
        Map<String, Object> map = new HashMap<>();
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (category.getParentId() != null) {
            categoryMapper.deleteByPrimaryKey(id);
            map.put("message", "删除成功");
            map.put("status", 200);
            return map;
        } else {
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andParentIdEqualTo(id);
            List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
            if(categoryList.size() == 0){
                categoryMapper.deleteByPrimaryKey(id);
                map.put("message", "删除成功");
                map.put("status", 200);
                return map;
            }else {
                map.put("message", "一级类别下有二级类别不能删除");
                map.put("status", 400);
                return map;
            }
        }
    }
    //根据id查询用户信息
    @Override
    public Category queryById(int id) {
        return categoryMapper.selectByPrimaryKey(id);
    }
    //修改
    @Override
    public Map<String, Object> update(int id, String cateName, Integer parentId) {
        Map<String, Object> map = new HashMap<>();
        if(parentId==null){
            int i = categoryMapper.updateByPrimaryKey(new Category(id, cateName, 1, parentId));
            if(i==1){
                map.put("message", "修改成功");
                map.put("status", 200);
                return map;
            }else {
                map.put("message", "修改失败");
                map.put("status", 400);
                return map;
            }
        }else {
            int i = categoryMapper.updateByPrimaryKey(new Category(id, cateName, 2, parentId));
            if (i==1){
                map.put("message", "修改成功");
                map.put("status", 200);
                return map;
            }else {
                map.put("message", "修改失败");
                map.put("status", 400);
                return map;
            }
        }
    }
}
